package game.adm.dao.impl;

import game.adm.dao.ConfigDAO;
import game.adm.dao.dto.ConfigDTO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import projectx.core.base.ProjectXSystemException;
import projectx.core.dao.AbstractDAO;
import projectx.core.dao.DataAccessException;
import projectx.core.dao.ExceptionHandler;
import projectx.core.dao.impl.PreparedStatementBuilder;

public class ConfigDAOImpl extends AbstractDAO implements ConfigDAO {
	
	public enum field {
		cofigid,
		websitename,
		helplink,
		userscale,
		createtime,
		updatetime,
		createip,
		updateip
	}
	public ConfigDAOImpl() throws ProjectXSystemException {
		super();
	}

	@Override
	public ConfigDTO getConfig() throws DataAccessException{
		ResultSet rs = null;
		PreparedStatement ps = null; 
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get("ADM_ConfigDAO_SELECT");
			sql = _builder.getSql(null, null);
			ps = _builder.createStatement(_conn, sql, false, false);
			debugSql();
			System.err.println("ConfigDAO_SELECT="+sql);
			rs = ps.executeQuery(); 

			if (rs.next()) {
				String cofigid = getString(rs.getString(field.cofigid.ordinal()+1));
				String websitename = getString(rs.getString(field.websitename.ordinal()+1));
				String helplink = getString(rs.getString(field.helplink.ordinal()+1));
				double userscale = (rs.getDouble(field.userscale.ordinal()+1));
				String createtime = rs.getString(field.createtime.ordinal()+1);
				String updatetime = rs.getString(field.updatetime.ordinal()+1);
				String createip = getString(rs.getString(field.createip.ordinal()+1));
				String updateip = getString(rs.getString(field.updateip.ordinal()+1));
				ConfigDTO dto = new ConfigDTO();
				dto.setCofigid(cofigid);
				dto.setWebsitename(websitename);
				dto.setHelplink(helplink);
				dto.setUserscale(userscale);
				dto.setCreatetime(createtime);
				dto.setUpdatetime(updatetime);
				dto.setCreateip(createip);
				dto.setUpdateip(updateip); 
				return dto;
			}
		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		return null;
	}

	@Override
	public boolean insert(ConfigDTO dto) throws DataAccessException {
	    if(getConfig()!=null)
		    this.empty();
			int result = 0;
			PreparedStatement ps = null;
			String sql = null;
			try {
				_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
						"ADM_ConfigDAO_INSERT");
				sql = _builder.getSql(null, null);
				ps = _builder.createStatement(_conn, sql, false, false);
				_builder.setParameter(ps, field.cofigid.ordinal()+1, dto.getCofigid(), Types.VARCHAR);
				_builder.setParameter(ps, field.websitename.ordinal()+1, dto.getWebsitename(), Types.VARCHAR);
				_builder.setParameter(ps, field.helplink.ordinal()+1, dto.getHelplink(), Types.VARCHAR);
				_builder.setParameter(ps, field.userscale.ordinal()+1, dto.getUserscale(), Types.DOUBLE);
				_builder.setParameter(ps, field.createtime.ordinal()+1, dto.getCreatetime(), Types.VARCHAR);
				_builder.setParameter(ps, field.updatetime.ordinal()+1, dto.getUpdatetime(), Types.VARCHAR);
				_builder.setParameter(ps, field.createip.ordinal()+1, dto.getCreateip(), Types.VARCHAR);
				_builder.setParameter(ps, field.updateip.ordinal()+1, dto.getUpdateip(), Types.VARCHAR);
				debugSql();
				result = ps.executeUpdate();

			} catch (Exception e) {
				errorSql(e);
				_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					logError(e);
				}
			}
			if (result == 0) {
				_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
			} else if (result > 1) {
				_exHandler.handleUpdateException(null,
						ExceptionHandler.MULTIUPDATED);
			}
			return true;
		}


	public boolean empty() throws DataAccessException {
			int result = 0;
			PreparedStatement ps = null;
			String sql = null;
			try {
				_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
						"ADM_ConfigDAO_EMPTY");
				sql = _builder.getSql(null, null);
				ps = _builder.createStatement(_conn, sql, false, false);
				debugSql();
				result = ps.executeUpdate();

			} catch (Exception e) {
				errorSql(e);
				_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
				} catch (SQLException e) {
					logError(e);
				}
			}
			if (result == 0) {
				_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
			} else if (result > 1) {
				_exHandler.handleUpdateException(null,
						ExceptionHandler.MULTIUPDATED);
			}
			return true;
		}

	
	@Override
	public boolean update(ConfigDTO dto) throws DataAccessException {
		if(getConfig()==null)
			return insert(dto);
		int result = 0;
		PreparedStatement ps = null;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"ADM_ConfigDAO_UPDATE");
			sql = _builder.getSql(null, null);
			ps = _builder.createStatement(_conn, sql, false, false);
			int i =1;
			_builder.setParameter(ps, i++, dto.getCofigid(), Types.VARCHAR);
			_builder.setParameter(ps, i++, dto.getWebsitename(), Types.VARCHAR);
			_builder.setParameter(ps, i++, dto.getHelplink(), Types.VARCHAR);
			_builder.setParameter(ps, i++, dto.getUserscale(), Types.DOUBLE);
			_builder.setParameter(ps, i++, dto.getUpdatetime(), Types.VARCHAR);
			_builder.setParameter(ps, i++, dto.getUpdateip(), Types.VARCHAR);
			debugSql();
			result = ps.executeUpdate();

		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleUpdateException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (result == 0) {
			_exHandler.handleUpdateException(null, ExceptionHandler.NOTUPDATED);
		} else if (result > 1) {
			_exHandler.handleUpdateException(null,
					ExceptionHandler.MULTIUPDATED);
		}
		return true;
	}

}
